//
// # Gestión de empresas de FCT
//
// Programa para la gestión de la base de datos de empresas de FCT
//
var http = require('http');
var path = require('path');

var express = require('express');

//Passwords
var config = require('./config.js');

// for couch
var cradle = require('cradle');
var host = 'https://pprieto.cloudant.com';
var port = 443;
var credentials = {username: config.couchdb.username, password: config.couchdb.password };
var local=false;
var db;
if(local===true) {
  db = new(cradle.Connection)().database('crud');
}
else {
  db = new(cradle.Connection)(host, port, {auth: credentials}).database('crud');
}

//
// ## SimpleServer `SimpleServer(obj)`
//
// Creates a new instance of SimpleServer with the following options:
//  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
//
var app = express();

// global data
var contentType = 'application/html';

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});


app.use(express.static(path.resolve(__dirname, 'client')));


/* starting main main main page */
app.get('/main/', function(req, res){
  var ctype;
  
  var view = 'welcome';
  
  var options = {};

  //ctype = acceptsXml(req);
  
  db.get(view, function(err, doc) {
      console.log(doc);
    /*res.header('content-type',ctype);
    res.render('index', {
      title: 'Home',
      site: baseUrl,
      items: doc
    });*/
  });
});


app.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  //var addr = app.address();
  //console.log("Server listening at", addr.address + ":" + addr.port);
});
